package loupan_utils

import (
	"hft/Database/admin"
	"regexp"
	"strconv"
	"strings"
	"time"
)

// 找出区域
func FindRegion(list []*admin.Region, findStr string) *admin.Region {
	for _, v := range list {
		if v.Name == findStr {
			return v
		}
	}
	return nil
}

// 获取经纬度
func GetLatLng(zb string) (float64, float64) {
	var s = strings.Split(zb, ",")
	var lng, _ = strconv.ParseFloat(s[0], 64)
	var lat, _ = strconv.ParseFloat(s[1], 64)
	return lng, lat
}

// 获取面积
func ExtractNumbers(input string) []int {
	var numbers []int
	re := regexp.MustCompile(`\d+`)
	matches := re.FindAllString(input, -1)

	for _, match := range matches {
		num, err := strconv.Atoi(match)
		if err == nil {
			numbers = append(numbers, num)
		}
	}

	if len(numbers) >= 3 {
		return []int{numbers[0], numbers[len(numbers)-1]}
	}

	if len(numbers) < 2 {
		return []int{0, 0}
	}

	return numbers
}

// 提取面积
func ExtractNumber(s string) float64 {
	// 去掉所有空字符串
	re := regexp.MustCompile(`\s+`)
	a := re.ReplaceAllString(s, "")

	// 提取数字
	re = regexp.MustCompile(`\d+`)
	numberStrings := re.FindAllString(a, -1)

	if len(numberStrings) == 0 {
		return 0
	}

	// 取最后一个数字
	lastNumberString := numberStrings[len(numberStrings)-1]

	// 判断数字后是否紧接着汉字"万"
	var str = string(lastNumberString[len(lastNumberString)-1])
	if str == "万" {
		lastNumberString = lastNumberString[:len(lastNumberString)-1]
		number, err := strconv.ParseFloat(lastNumberString, 64)
		if err != nil {
			return 0
		}
		return number * 1000
	}

	number, err := strconv.ParseFloat(lastNumberString, 64)
	if err != nil {
		return 0
	}

	return number
}

// 开盘时间
func ExtractTime(s string) int64 {
	// 提取数字
	re := regexp.MustCompile(`\d+`)
	numberStrings := re.FindAllString(s, -1)

	result := make([]string, 0, len(numberStrings))

	for i, numStr := range numberStrings {
		// 去除数字前导的零
		num, err := strconv.Atoi(numStr)
		if err != nil {
			continue
		}

		// 第二个和第三个数字位数为1，则在其前面拼接一个0
		if i == 1 && num < 10 && numStr[0] != '0' {
			result = append(result, "0"+numStr)
		} else if i == 2 && num < 10 && numStr[0] != '0' {
			result = append(result, "0"+numStr)
		} else {
			result = append(result, numStr)
		}
	}
	if len(result) == 2 {
		result = append(result, "01")
	} else if len(result) == 1 {
		result = append(result, "01", "01")
	}
	var timeStr = strings.Join(result, "")
	if timeStr == "" {
		return 0
	} else {
		var t, _ = time.Parse("20060102", timeStr)
		return t.Unix()
	}

}

// 获取物业用途
func GetProperties(labels string, proList []*admin.Property) []*admin.Property {
	var list = strings.Split(labels, ",")
	var pros = make([]*admin.Property, 0)
	if len(list) != 0 {
		for _, v := range list {
			var code, _ = strconv.Atoi(v)
			for _, j := range proList {
				if j.StatusCode == code {
					pros = append(pros, j)
				}
			}
		}
	}
	return pros
}

// 获取标签
func GetTags(labels string, proList []*admin.CommonTag) []*admin.CommonTag {
	var list = strings.Split(labels, ",")
	var pros = make([]*admin.CommonTag, 0)
	if len(list) != 0 {
		for _, v := range list {
			var code, _ = strconv.Atoi(v)
			for _, j := range proList {
				if int(j.ID) == code {
					pros = append(pros, j)
				}
			}
		}
	}
	return pros
}

func GetTese(labels string, proList []*admin.LouPanTeSe, bs string) []*admin.LouPanTeSe {
	var list = strings.Split(labels, ",")
	var pros = make([]*admin.LouPanTeSe, 0)
	var bsInt, _ = strconv.Atoi(bs)
	if len(list) != 0 {
		for _, v := range list {
			var code, _ = strconv.Atoi(v)
			for _, j := range proList {
				if int(j.ID) == code {
					pros = append(pros, j)
				}
			}
		}
		for _, v := range proList {
			if v.ID == 7 && bsInt == 1 {
				pros = append(pros, v)
			}
			if v.ID == 8 && bsInt == 3 {
				pros = append(pros, v)
			}
		}
	}

	return pros
}

// 获取户型
func GetRoomTypes(hcate string, rooms []*admin.RoomType) []*admin.RoomType {
	var hs = strings.Split(hcate, ",")
	var types = make([]*admin.RoomType, 0)
	for _, v := range hs {
		var id, _ = strconv.Atoi(v)
		for _, j := range rooms {
			if id == j.Code && id != 6 {
				types = append(types, j)
			}

		}
	}
	return types
}

// 找出所属区域
func GetBelongName(list []*admin.Region, regionLink string) string {

	var newLink = strings.Split(regionLink, ",")
	var newLink1 = newLink[2 : len(newLink)-1]
	var nameStr []string
	for _, v := range newLink1 {
		id, _ := strconv.Atoi(v)
		for _, j := range list {
			if id == int(j.ID) {
				nameStr = append(nameStr, j.Name)
			}
		}
	}
	return strings.Join(nameStr, "-")
}

// 找出地址
func FindAddress() {

}
